package bbkj.org.cn.imple;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import bbkj.org.cn.service.RestDemoService;
import lombok.extern.slf4j.Slf4j;

@Service
@DubboService
@Slf4j
public class RestDemoImple implements RestDemoService{

	@Override
	public String get() {
		random();
		return null;
	}

	@Override
	public String post() {
		random();
		return null;
	}

	@Override
	public String put() {
		random();
		return null;
	}

	@Override
	public String delete() {
		random();
		return null;
	}

	private void random() {
		int randomValue = (int) (Math.random()*100);
		log.info("当前概率值：{}",randomValue);
		//随机概率成功！	60%
		if(randomValue>40) {
			log.info("请求成功");
			return;
		}
		//随机概率超时！	20%
		if(randomValue<20) {
			log.info("延迟12s");
			int i=0;
			while(i++<12) {
				try {
					Thread.sleep(1000l);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return;
		}
		//随机概率失败！	20%
		throw new RuntimeException("请求失败");
	}

}
